package com.cy.pj.goods.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;

@Controller
@RequestMapping("/goods/")
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;
	
	/**查询业务*/
	@RequestMapping("doGoodsUI")
	public String doGoodsUI(Model model) {
		//调用业务层方法获取商品信息
		List<Goods> list = goodsService.findAll();
		 //将数据存储到请求作用域
		 model.addAttribute("goods", list);
		 
		 return "goods";//viewname
	}
	
	/**根据ID删除数据*/
	@RequestMapping("doDeleteById/{id}")
	public String doDeleteById(@PathVariable Integer id) {
		
		goodsService.deleteById(id);
		return "redirect:/goods/doGoodsUI";
	}
	
	/**添加数据*/
	@RequestMapping("doGoodsAddUI")
	public String doGoodsAddUI() {
		return "goods-add";
	}
	@RequestMapping("doSaveGoods")
	public String doSaveGoods(Goods goods) {
		goodsService.saveGoods(goods);
		return "redirect:/goods/doGoodsUI";
	}
	
	/**根据ID修改数据*/
	@RequestMapping("doFindById/{id}")
	public String doFindById(@PathVariable Integer id,Model model) {
		Goods goods=goodsService.findById(id);
		model.addAttribute("goods",goods);
		return "goods-update";
	}
	
	@RequestMapping("doUpdateGoods")
	public String doGoodsUpdate(Goods goods) {
		goodsService.updateGoods(goods);
		return "redirect:/goods/doGoodsUI";
	}
}
